package edu.uoc.tds.pac4.beansGestors;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import edu.uoc.tds.pac4.DAO.GestorDisc;
import edu.uoc.tds.pac4.DAO.Select;
import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.beans.EstIngressos;
import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.beans.Universitat;
import edu.uoc.tds.pac4.common.GestioEventsException;

public class GestorEstIngressos extends GestorDisc{
	
	private static final long serialVersionUID = 1L;
	protected final static String getColumnes = "ie.u_nom, ie.c_nom, ie.t_nom, ie.inscrits*ie.e_preu as ingres";
	
	public Vector<EstIngressos> getInforme(Universitat uni, Centre cen, TipusEvent te, Double ingresMinim, Double ingresMaxim) throws GestioEventsException{
		Vector<EstIngressos> vec = new Vector<EstIngressos>();
		// Opcions de filtre
		String sqlWhere = "";
		boolean isFirstWhere = true;

		if(cen != null)
		{
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " ie.c_codi_centre = " + this.putString(cen.getCodi());
		}
		else if(uni != null)
		{
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " ie.u_codi_universitat = " + this.putString(uni.getCodi()) ;
		}
		if(te != null) {
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " ie.t_codi_tevent = " + this.putString(te.getCodi());
		}
		
		if(ingresMinim>0) {
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " (ie.inscrits*ie.e_preu) >= " + this.putNumber(ingresMinim) + " ";
		}
		
		if(ingresMaxim>ingresMinim) {
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " (ie.inscrits*ie.e_preu) <= " + this.putNumber(ingresMaxim) + " ";
		}
		
		Select sel = new Select("InscritsAEvent ie ", sqlWhere);
				
		sel.addLeftJoin(" participen pt ", " pt.codi_event = ie.e_codi_event AND pt.assitencia='t' ");

		sel.addCamp(getColumnes);
		sel.setOrderBy("ie.e_inici", "ASC");
		
		ResultSet rs = this.llegir(sel);
		
		if(rs!=null){
			try {
				do {
					EstIngressos eI = new EstIngressos();
					eI.buildFromRS(rs);
					vec.add(eI);
				} while(rs.next());
				rs.close();
			} 
			catch (SQLException e) 
			{
				throw new GestioEventsException(GestioEventsException.Error_BD);
			}
		}
		return vec;
	}
}
